<?php
include 'header.php';

@$orderType = $_REQUEST['orderType'];

@$pageNum = $_REQUEST['pageNum'];
$pageNum = pageNum($pageNum);
$pageSize = 10;

if($orderType == 'now'){
	$sql = "select id,name,goodsPrice,payPrice,startDate,payDate from `order` where pay='否'";
	@$noPayOrders = dbSelectPage($sql,$pageNum,$pageSize);
}else if($orderType == 'history'){
	$sql = "select id,name,goodsPrice,payPrice,startDate,payDate from `order` where pay='是'";
	@$payOrders = dbSelectPage($sql,$pageNum,$pageSize);
}else{
	$sql = 'select id,name,goodsPrice,payPrice,startDate,payDate,pay from `order`';
	@$allOrders = dbSelectPage($sql,$pageNum,$pageSize);
}
@$count = dbSelectAll($sql);
$num = ceil(count($count)/$pageSize);


?>
		<div class='container'>
   			<div class='row'>
   				<div class='col-xs-2'>
   					<a href='index.php' class='btn btn-default btn-block'>返回首页</a>
   					<a href="addOrder.php" class='btn btn-default btn-block'>新建订单</a>
   					<?php if($orderType == 'now'){?>
   						<a href="orderList.php?orderType=all" class='btn btn-default btn-block'>全部订单</a>
   						<a href="orderList.php?orderType=history" class='btn btn-default btn-block'>历史订单</a>
   					<?php }else if($orderType == 'history'){?>
   						<a href="orderList.php?orderType=all" class='btn btn-default btn-block'>全部订单</a>
   						<a href="orderList.php?orderType=now" class='btn btn-default btn-block'>当前订单</a>
   					<?php }else{?>
   						<a href="orderList.php?orderType=now" class='btn btn-default btn-block'>当前订单</a>
   						<a href="orderList.php?orderType=history" class='btn btn-default btn-block'>历史订单</a>
   					<?php }?>
   				</div>
   				<div class='col-xs-10'>
   					<div class='panel panel-primary'>
   						<div class='panel-heading'>
   							<?php if($orderType == 'now'){?>
   								<div class='panel-title'>当前订单列表</div>
   							<?php }else if($orderType == 'history'){?>
   								<div class='panel-title'>历史订单列表</div>
   							<?php }else{?>
   								<div class='panel-title'>所有订单列表</div>
   							<?php }?>
   						</div>
   						<table class='orderTable table table-bordered text-center'>
   							<tr>
   								<th class='text-center'>名称</th>
   								<th class='text-center'>应付金额</th>
   								<th class='text-center'>实付金额</th>
   								<th class='text-center'>下单时间</th>
   								<th class='text-center'>结账时间</th>
   								<th class='text-center'>商品明细</th>
   								<th class='text-center'>编辑</th>
   							</tr>
   							<?php 
	   							if($orderType == 'now'){
	   								foreach($noPayOrders as $data){
	   									@$goods_orders = dbSelectAll('select imgUrl,goods.name as gName,type.name as tName,sellPrice,`count` from goods_order 
																	join goods on goods.id=goods_order.goodsId 
																	join `type` on goods.typeId=type.id where goods_order.orderId=:id',array('id'=>$data['id']));?>
	   									<tr id='orderTtr<?php echo $data['id'];?>'>
	   										<td>
	   											<a href="index.php?noPayOrderId=<?php echo $data['id'];?>">
	   												<?php echo $data['name'];?>
	   											</a>
	   										</td>
			   								<td><?php echo $data['goodsPrice'];?></td>
			   								<td><?php echo $data['payPrice'];?></td>
			   								<td><?php echo $data['startDate'];?></td>
			   								<td><?php echo $data['payDate'];?></td>
			   								<td>
			   									<a href='#goods_order<?php echo $data['id'];?>' data-toggle='modal'>商品明细</a>
			   									<div class='modal' id='goods_order<?php echo $data['id'];?>'>
			   										<div class='modal-dialog'>
			   											<div class='modal-content'>
				   											<div class='modal-header'>
				   												<h4 class='modal-title'>
				   													[<?php echo $data['name']?>]所有商品
				   												</h4>
				   											</div>
				   											<div class='modal-body'>
					   											<table class='table table-bordered text-center'>
					   												<tr>
					   													<th class='text-center'>图片</th>
					   													<th class='text-center'>名称</th>
					   													<th class='text-center'>单价</th>
					   													<th class='text-center'>数量</th>
					   													<th class='text-center'>类型</th>
					   												</tr>
					   												<?php foreach($goods_orders as $goods_order){?>
					   													<tr>
					   														<td><img src='img/<?php echo $goods_order['imgUrl'];?>' class='goodsImg' /></td>
					   														<td><?php echo $goods_order['gName'];?></td>
					   														<td><?php echo $goods_order['sellPrice'];?></td>
					   														<td><?php echo $goods_order['count'];?></td>
					   														<td><?php echo $goods_order['tName'];?></td>
					   													</tr>
					   												<?php }?>
					   											</table>
					   										</div>
				   											<div class='modal-footer text-right'>
				   												<a href='#' class='btn btn-default' data-dismiss='modal'>返回</a>
				   											</div>
			   											</div>
			   										</div>
			   									</div>
			   								</td>
			   								<td>
			   									<a href='#' id='delOrder<?php echo $data['id'];?>'>取消订单</a>
			   								</td>
			   							</tr>
	   								<?php }
	   							}else if($orderType == 'history'){
	   								foreach($payOrders as $data){
	   									@$goods_orders = dbSelectAll('select imgUrl,goods.name as gName,type.name as tName,sellPrice,`count` from goods_order 
																	join goods on goods.id=goods_order.goodsId 
																	join `type` on goods.typeId=type.id where goods_order.orderId=:id',array('id'=>$data['id']));?>
	   									<tr>
			   								<td><?php echo $data['name'];?></td>
			   								<td><?php echo $data['goodsPrice'];?></td>
			   								<td><?php echo $data['payPrice'];?></td>
			   								<td><?php echo $data['startDate'];?></td>
			   								<td><?php echo $data['payDate'];?></td>
			   								<td>
			   									<a href='#goods_order<?php echo $data['id'];?>' data-toggle='modal'>商品明细</a>
			   									<div class='modal' id='goods_order<?php echo $data['id'];?>'>
			   										<div class='modal-dialog'>
			   											<div class='modal-content'>
				   											<div class='modal-header'>
				   												<h4 class='modal-title'>
				   													[<?php echo $data['name']?>]所有商品
				   												</h4>
				   											</div>
				   											<div class='modal-body'>
					   											<table class='table table-bordered text-center'>
					   												<tr>
					   													<th class='text-center'>图片</th>
					   													<th class='text-center'>名称</th>
					   													<th class='text-center'>单价</th>
					   													<th class='text-center'>数量</th>
					   													<th class='text-center'>类型</th>
					   												</tr>
					   												<?php foreach($goods_orders as $goods_order){?>
					   													<tr>
					   														<td><img src='img/<?php echo $goods_order['imgUrl'];?>' class='goodsImg' /></td>
					   														<td><?php echo $goods_order['gName'];?></td>
					   														<td><?php echo $goods_order['sellPrice'];?></td>
					   														<td><?php echo $goods_order['count'];?></td>
					   														<td><?php echo $goods_order['tName'];?></td>
					   													</tr>
					   												<?php }?>
					   											</table>
					   										</div>
				   											<div class='modal-footer text-right'>
				   												<a href='#' class='btn btn-default' data-dismiss='modal'>返回</a>
				   											</div>
			   											</div>
			   										</div>
			   									</div>
			   									<?php @$clientele = dbSelect('select clientele.name as cName,phone,location from clientele join `order` on clientele.id=`order`.clienteleId where `order`.id=:id',
																			array('id'=>$data['id']));	?>
			   									<a href='#clientele_order<?php echo $data['id'];?>' data-toggle='modal'>客户明细</a>
			   									<div class='modal' id='clientele_order<?php echo $data['id'];?>'>
				   										<div class='modal-dialog'>
				   											<div class='modal-content'>
					   											<div class='modal-header'>
					   												<h4 class='modal-title'>
					   													[<?php echo $data['name']?>]所属客户
					   												</h4>
					   											</div>
					   											<div class='modal-body'>
						   											<table class='table table-bordered text-center'>
						   												<tr>
						   													<th class='text-center'>姓名</th>
						   													<th class='text-center'>电话</th>
						   													<th class='text-center'>地址</th>
						   												</tr>
					   													<tr>
					   														<td><?php echo $clientele['cName'];?></td>
					   														<td><?php echo $clientele['phone'];?></td>
					   														<td><?php echo $clientele['location'];?></td>
					   													</tr>
						   											</table>
						   										</div>
					   											<div class='modal-footer text-right'>
					   												<a href='#' class='btn btn-default' data-dismiss='modal'>返回</a>
					   											</div>
				   											</div>
				   										</div>
				   									</div>
			   								</td>
			   								<td>----</td>
			   							</tr>
	   								<?php }
	   							}else{
	   								foreach($allOrders as $data){
	   									@$goods_orders = dbSelectAll('select imgUrl,goods.name as gName,type.name as tName,sellPrice,`count` from goods_order 
																	join goods on goods.id=goods_order.goodsId 
																	join `type` on goods.typeId=type.id where goods_order.orderId=:id',array('id'=>$data['id']));?>
	   									<tr id='orderTtr<?php echo $data['id'];?>'>
	   										<?php if($data['pay'] == "是"){?>
				   								<td><?php echo $data['name'];?></td>
	   										<?php }else{?>
				   								<td>
		   											<a href="index.php?noPayOrderId=<?php echo $data['id'];?>">
		   												<?php echo $data['name'];?>
		   											</a>
		   										</td>
	   										<?php }?>
			   								<td><?php echo $data['goodsPrice'];?></td>
			   								<td><?php echo $data['payPrice'];?></td>
			   								<td><?php echo $data['startDate'];?></td>
			   								<td><?php echo $data['payDate'];?></td>
			   								<td>
			   									<a href='#goods_order<?php echo $data['id'];?>' data-toggle='modal'>商品明细</a>
			   									<div class='modal' id='goods_order<?php echo $data['id'];?>'>
			   										<div class='modal-dialog'>
			   											<div class='modal-content'>
				   											<div class='modal-header'>
				   												<h4 class='modal-title'>
				   													[<?php echo $data['name']?>]所有商品
				   												</h4>
				   											</div>
				   											<div class='modal-body'>
					   											<table class='table table-bordered text-center'>
					   												<tr>
					   													<th class='text-center'>图片</th>
					   													<th class='text-center'>名称</th>
					   													<th class='text-center'>单价</th>
					   													<th class='text-center'>数量</th>
					   													<th class='text-center'>类型</th>
					   												</tr>
					   												<?php foreach($goods_orders as $goods_order){?>
					   													<tr>
					   														<td><img src='img/<?php echo $goods_order['imgUrl'];?>' class='goodsImg' /></td>
					   														<td><?php echo $goods_order['gName'];?></td>
					   														<td><?php echo $goods_order['sellPrice'];?></td>
					   														<td><?php echo $goods_order['count'];?></td>
					   														<td><?php echo $goods_order['tName'];?></td>
					   													</tr>
					   												<?php }?>
					   											</table>
					   										</div>
				   											<div class='modal-footer text-right'>
				   												<a href='#' class='btn btn-default' data-dismiss='modal'>返回</a>
				   											</div>
			   											</div>
			   										</div>
			   									</div>
			   									<?php if($data['pay'] == "是"){
													@$clientele = dbSelect('select clientele.name as cName,phone,location from clientele join `order` on clientele.id=`order`.clienteleId where `order`.id=:id',
																			array('id'=>$data['id']))			   										
			   									?>
			   										<a href='#clientele_order<?php echo $data['id'];?>' data-toggle='modal'>客户明细</a>
			   										<div class='modal' id='clientele_order<?php echo $data['id'];?>'>
				   										<div class='modal-dialog'>
				   											<div class='modal-content'>
					   											<div class='modal-header'>
					   												<h4 class='modal-title'>
					   													[<?php echo $data['name']?>]所属客户
					   												</h4>
					   											</div>
					   											<div class='modal-body'>
						   											<table class='table table-bordered text-center'>
						   												<tr>
						   													<th class='text-center'>姓名</th>
						   													<th class='text-center'>电话</th>
						   													<th class='text-center'>地址</th>
						   												</tr>
					   													<tr>
					   														<td><?php echo $clientele['cName'];?></td>
					   														<td><?php echo $clientele['phone'];?></td>
					   														<td><?php echo $clientele['location'];?></td>
					   													</tr>
						   											</table>
						   										</div>
					   											<div class='modal-footer text-right'>
					   												<a href='#' class='btn btn-default' data-dismiss='modal'>返回</a>
					   											</div>
				   											</div>
				   										</div>
				   									</div>
			   									<?php }?>
			   								</td>
			   								<td>
			   									<?php if($data['pay'] == '是'){?>
			   										----
			   									<?php }else{?>
			   										<a href='#' id='delOrder<?php echo $data['id'];?>'>取消订单</a>
			   									<?php }?>
			   								</td>
			   							</tr>
	   								<?php }
	   							}
	   						?>
   						</table>
   						<div class='panel-body'>
   							<div class='pageDiv text-center'>
   								<a href='?pageNum=<?php echo '1';?>&&orderType=<?php echo $orderType;?>' class='btn btn-default'>首页</a>
								<?php if($pageNum > 1){?>
	   								<a href='?pageNum=<?php echo $pageNum -1;?>&&orderType=<?php echo $orderType;?>' class='btn btn-default'>上一页</a>
								<?php }else{?>
									上一页
								<?php }?>
								<?php if($pageNum < $num){?>
	   								<a href='?pageNum=<?php echo $pageNum +1;?>&&orderType=<?php echo $orderType;?>' class='btn btn-default'>下一页</a>
								<?php }else{?>
									下一页
								<?php }?>
   								<a href='?pageNum=<?php echo $num;?>&&orderType=<?php echo $orderType;?>' class='btn btn-default'>尾页</a>
   							</div>
   						</div>
   					</div>
   				</div>
   			</div>
   		</div>
   	</body>
</html>